package com.xiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.projo.Course;
import com.xiaohai.mapper.CourseMapper;
import com.xiaohai.projo.dto.CourseDto;
import com.xiaohai.projo.Score;
import com.xiaohai.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.service.ScoreService;
import com.xiaohai.service.StudentService;
import com.xiaohai.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohai
 * @since 2024-02-24
 */
@Slf4j
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Long getByTeacherId(Integer id) {
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(id != null, Course::getTeaId, id);
        return super.count(courseLambdaQueryWrapper);
    }

    @Override
    public Page<CourseDto> myList(Integer currentPage, Integer pageSize, String courseName, String teacherName, Integer identity,String account) {
        Page<CourseDto> page = new Page<>(currentPage, pageSize);
        Page<CourseDto> list = null;
        if (identity.equals(1)) {// 管理员
            list = courseMapper.list(page, courseName, teacherName);
        }
        if (identity.equals(2)) {// 学生账号
            list = courseMapper.listByStu(page, courseName, teacherName, account);
        }
        if (identity.equals(3)) {// 教师账号
            list = courseMapper.listByTea(page, courseName, account);
        }
        return list;
    }

    @Override
    public Course detail(String id) {
        CourseDto course = courseMapper.getByid(id);
        return course;
    }

    @Override
    public boolean add(Course course) {
//        String account = (String) request.getSession().getAttribute("account");
//        Teacher teacher = teacherService.getInfoByAccount(account);
//        course.setTeaId(teacher.getId());
        return super.save(course);
    }

    @Override
    public Boolean myUpdate(Course course) {
        return super.updateById(course);
    }

    @Override
    public boolean delete(Integer id) {
        LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scoreLambdaQueryWrapper.eq(id != null, Score::getCourceId, id);
        List<Score> list = scoreService.list(scoreLambdaQueryWrapper);

        if (list.isEmpty()) {
            return super.removeById(id);
        }
        return false;
    }
}
